//package com.cus.shd.sharingjdbc.demo;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
//import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
//import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
//import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
//import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
//import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
//import org.springframework.stereotype.Service;
//
//import javax.sql.DataSource;
//import java.sql.Connection;
//import java.sql.PreparedStatement;
//import java.util.Collections;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Properties;
//
//@Service
//public class SharingJdbcJavaDemo {
//
//    public void com(){
//        // Configure actual data sources
//        Map<String, DataSource> dataSourceMap = new HashMap<>();
//
//        // Configure the first data source
//        DruidDataSource dataSource1 = new DruidDataSource();
//        dataSource1.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        dataSource1.setUrl("jdbc:mysql://localhost:3306/world1?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8");
//        dataSource1.setUsername("root");
//        dataSource1.setPassword("123456");
//        dataSourceMap.put("ds0", dataSource1);
//
//// Configure the second data source
//        DruidDataSource dataSource2 = new DruidDataSource();
//        dataSource2.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        dataSource2.setUrl("jdbc:mysql://localhost:3306/world2?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8");
//        dataSource2.setUsername("root");
//        dataSource2.setPassword("123456");
//        dataSourceMap.put("ds1", dataSource2);
//
//        // Configure order table rule
//        ShardingTableRuleConfiguration orderTableRuleConfig = new ShardingTableRuleConfiguration("t_order", "ds${0..1}.t_order_${0..2}");
//
//        // Configure database sharding strategy
//        orderTableRuleConfig.setDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "dbShardingAlgorithm"));
//
//        // Configure table sharding strategy
//        orderTableRuleConfig.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "tableShardingAlgorithm"));
//
//        orderTableRuleConfig.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("SNOWFLAKE", "cid"));
//        // Omit t_order_item table rule configuration ...
//        // ..
//
//        // Configure sharding rule
//        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
//        shardingRuleConfig.getTables().add(orderTableRuleConfig);
//
//        // Configure database sharding algorithm
//        Properties dbShardingAlgorithmrProps = new Properties();
//        dbShardingAlgorithmrProps.setProperty("algorithm-expression", "ds${user_id % 2}");
//        shardingRuleConfig.getShardingAlgorithms().put("dbShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("INLINE", dbShardingAlgorithmrProps));
//
//        // Configure table sharding algorithm
//        Properties tableShardingAlgorithmrProps = new Properties();
//        tableShardingAlgorithmrProps.setProperty("sharding-ranges","100,500");
////        tableShardingAlgorithmrProps.setProperty("algorithm-expression", "order_${order_id % 3}");
////        tableShardingAlgorithmrProps.setProperty("allow-range-query-with-inline-sharding","true");
//        shardingRuleConfig.getShardingAlgorithms().put("tableShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("BOUNDARY_RANGE", tableShardingAlgorithmrProps));
//        // Create ShardingSphereDataSource
//        try {
//            Properties props=new Properties();
//            props.put("sql.show", "true");
//            DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton(shardingRuleConfig), props);
////              String sql = "SELECT * FROM user WHERE user_id=? AND order_id >= ?";
////            try (
////                    Connection conn = dataSource.getConnection();
////                    PreparedStatement ps = conn.prepareStatement(sql)) {
////                ps.setInt(1, 10);
////                ps.setLong(2, 499L);
////                try (ResultSet rs = ps.executeQuery()) {
////                    while(rs.next()) {
////                        // ...
////                        System.out.println("userid=" + rs.getObject("user_id"));
////                        System.out.println("name=" + rs.getObject("name"));
////                        System.out.println("orderid=" + rs.getObject("order_id"));
////                    }
////                }
////            }
////            for(int i =0;i<100;i++) {
//                String sql = "INSERT INTO t_order(sname,age,user_id,order_id,cid) values(?,?,?,?,?)";
//                Connection conn = dataSource.getConnection();
//                PreparedStatement ps = conn.prepareStatement(sql);
//                ps.setString(1, "cfx");
//                int userid = (int) (Math.random() * 1000);
//                Long orderId = Long.valueOf((int) (Math.random() * 1000));
//                ps.setInt(2, 10);
//                ps.setInt(3, userid);
//                ps.setLong(4, orderId);
//                ps.setString(5,"");
//                ps.executeUpdate();
////            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        System.out.println("abc");
//    }
//}
